home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / RGB2GIF.C < prev    next >
C/C++ Source or Header  |  1991-06-17  |  9KB  |  293 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jun. 1991   *
  5. ******************************************************************************
  6. * Program to convert 24bits RGB files to GIF format.                 *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -c #colors : in power of two, i.e. 7 will allow upto 128 colors in output. *
  10. * -1 : one file holding RGBRGB.. triples of bytes                 *
  11. * -s Width Height : specifies size of raw image.                             *
  12. * -h : on line help.                                 *
  13. ******************************************************************************
  14. * History:                                     *
  15. * 15 Jun 91 - Version 1.0 by Gershon Elber.                     *
  16. *****************************************************************************/
  17.  
  18. #ifdef __MSDOS__
  19. #include <graphics.h>
  20. #include <stdlib.h>
  21. #include <alloc.h>
  22. #include <io.h>
  23. #include <dos.h>
  24. #include <bios.h>
  25. #endif /* __MSDOS__ */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include "gif_lib.h"
  32. #include "getarg.h"
  33.  
  34. #define PROGRAM_NAME    "RGB2Gif"
  35.  
  36. #ifdef __MSDOS__
  37. extern unsigned int
  38.     _stklen = 16384;                 /* Increase default stack size. */
  39. #endif /* __MSDOS__ */
  40.  
  41. #ifdef SYSV
  42. static char *VersionStr =
  43.     "Gif library module,\t\tGershon Elber\n\
  44.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  45. static char
  46.     *CtrlStr = "RGB2Gif q%- c%-#Colors!d 1%- s!-Width|Height!d!d h%- RGBFile!*s";
  47. #else
  48. static char
  49.     *VersionStr =
  50.     PROGRAM_NAME
  51.     GIF_LIB_VERSION
  52.     "    Gershon Elber,    "
  53.     __DATE__ ",   " __TIME__ "\n"
  54.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  55. static char
  56.     *CtrlStr =
  57.     PROGRAM_NAME
  58.     " q%- c%-#Colors!d 1%- s!-Width|Height!d!d h%- RGBFile!*s";
  59. #endif /* SYSV */
  60.  
  61. /* Make some variables global, so we could access them faster: */
  62. static int
  63.     ColorFlag = FALSE,
  64.     ExpNumOfColors = 8,
  65.     OneFileFlag = FALSE,
  66.     HelpFlag = FALSE,
  67.     ColorMapSize = 256;
  68.  
  69. static void LoadRGB(char *FileName,
  70.             int OneFileFlag,
  71.             GifByteType **RedBuffer,
  72.             GifByteType **GreenBuffer,
  73.             GifByteType **BlueBuffer,
  74.             int Width, int Height);
  75. static void SaveGif(GifByteType *OutputBuffer,
  76.             GifColorType *OutputColorMap,
  77.             int ExpColorMapSize, int Width, int Height);
  78. static void QuitGifError(GifFileType *GifFile);
  79.  
  80. /******************************************************************************
  81. * Interpret the command line and scan the given GIF file.              *
  82. ******************************************************************************/
  83. void main(int argc, char **argv)
  84. {
  85.     int    Error, NumFiles, Width, Height, SizeFlag;
  86.     char **FileName = NULL;
  87.     GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL,
  88.     *OutputBuffer = NULL;
  89.     GifColorType *OutputColorMap = NULL;
  90.  
  91.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  92.         &ColorFlag, &ExpNumOfColors, &OneFileFlag,
  93.         &SizeFlag, &Width, &Height, &HelpFlag,
  94.         &NumFiles, &FileName)) != FALSE ||
  95.         (NumFiles > 1 && !HelpFlag)) {
  96.     if (Error)
  97.         GAPrintErrMsg(Error);
  98.     else if (NumFiles > 1)
  99.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  100.     GAPrintHowTo(CtrlStr);
  101.     exit(1);
  102.     }
  103.  
  104.     if (HelpFlag) {
  105.     fprintf(stderr, VersionStr);
  106.     GAPrintHowTo(CtrlStr);
  107.     exit(0);
  108.     }
  109.  
  110.     ColorMapSize = 1 << ExpNumOfColors;
  111.  
  112.     if (NumFiles == 1) {
  113.     LoadRGB(*FileName, OneFileFlag,
  114.         &RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height);
  115.     }
  116.     else {
  117.     LoadRGB(NULL, OneFileFlag,
  118.         &RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height);
  119.     }
  120.  
  121.     if ((OutputColorMap = (GifColorType *) malloc(ColorMapSize *
  122.                           sizeof(GifColorType))) == NULL ||
  123.     (OutputBuffer = (GifByteType *) malloc(Width * Height *
  124.                         sizeof(GifByteType))) == NULL)
  125.     GIF_EXIT("Failed to allocate memory required, aborted.");
  126.  
  127.     if (QuantizeBuffer(Width, Height, &ColorMapSize,
  128.                RedBuffer, GreenBuffer, BlueBuffer,
  129.                OutputBuffer, OutputColorMap) == GIF_ERROR)
  130.     QuitGifError(NULL);
  131.     free((char *) RedBuffer);
  132.     free((char *) GreenBuffer);
  133.     free((char *) BlueBuffer);
  134.  
  135.     SaveGif(OutputBuffer, OutputColorMap, ExpNumOfColors, Width, Height);
  136. }
  137.  
  138. /******************************************************************************
  139. * Load RGB file into internal frame buffer.                      *
  140. ******************************************************************************/
  141. static void LoadRGB(char *FileName,
  142.             int OneFileFlag,
  143.             GifByteType **RedBuffer,
  144.             GifByteType **GreenBuffer,
  145.             GifByteType **BlueBuffer,
  146.             int Width, int Height)
  147. {
  148.     int i, j;
  149.     unsigned long Size;
  150.     GifByteType *RedP, *GreenP, *BlueP;
  151.     FILE *f[3];
  152.  
  153.     Size = ((long) Width) * Height * sizeof(GifByteType);
  154. #ifdef __MSDOS__
  155.     if (Size > 65500L)
  156.     GIF_EXIT("Can't allocate more than 64k.");
  157. #endif /* __MSDOS__ */
  158.  
  159.     if ((*RedBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
  160.     (*GreenBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
  161.     (*BlueBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL)
  162.     GIF_EXIT("Failed to allocate memory required, aborted.");
  163.  
  164.     RedP = *RedBuffer;
  165.     GreenP = *GreenBuffer;
  166.     BlueP = *BlueBuffer;
  167.  
  168.     if (FileName != NULL) {
  169.     char OneFileName[80];
  170.  
  171.     if (OneFileFlag) {
  172. #ifdef __MSDOS__
  173.         if ((f[0] = fopen(FileName, "rb")) == NULL)
  174. #else
  175.         if ((f[0] = fopen(FileName, "r")) == NULL)
  176. #endif /* __MSDOS__ */
  177.         GIF_EXIT("Can't open input file name.");
  178.     }
  179.     else {
  180.         static char *Postfixes[] = { ".R", ".G", ".B" };
  181.  
  182.         for (i = 0; i < 3; i++) {
  183.         strcpy(OneFileName, FileName);
  184.         strcat(OneFileName, Postfixes[i]);
  185.  
  186. #ifdef __MSDOS__
  187.         if ((f[i] = fopen(OneFileName, "rb")) == NULL)
  188. #else
  189.         if ((f[i] = fopen(OneFileName, "r")) == NULL)
  190. #endif /* __MSDOS__ */
  191.             GIF_EXIT("Can't open input file name.");
  192.         }
  193.     }
  194.     }
  195.     else {
  196.     OneFileFlag = TRUE;
  197.  
  198. #ifdef __MSDOS__
  199.     setmode(0, O_BINARY);
  200. #endif /* __MSDOS__ */
  201.  
  202.     f[0] = stdin;
  203.     }
  204.  
  205.     GifQprintf("\n%s: RGB image:     ", PROGRAM_NAME);
  206.  
  207.     if (OneFileFlag) {
  208.     GifByteType *Buffer, *BufferP;
  209.  
  210.     if ((Buffer = (GifByteType *) malloc(Width * 3)) == NULL)
  211.         GIF_EXIT("Failed to allocate memory required, aborted.");
  212.  
  213.     for (i = 0; i < Height; i++) {
  214.         GifQprintf("\b\b\b\b%-4d", i);
  215.         if (fread(Buffer, Width * 3, 1, f[0]) != 1)
  216.         GIF_EXIT("Input file(s) terminated prematurly.");
  217.         for (j = 0, BufferP = Buffer; j < Width; j++) {
  218.         *RedP++ = *BufferP++;
  219.         *GreenP++ = *BufferP++;
  220.         *BlueP++ = *BufferP++;
  221.         }
  222.     }
  223.  
  224.     free((char *) Buffer);
  225.     fclose(f[0]);
  226.     }
  227.     else {
  228.     for (i = 0; i < Height; i++) {
  229.         GifQprintf("\b\b\b\b%-4d", i);
  230.         if (fread(RedP, Width, 1, f[0]) != 1 ||
  231.         fread(GreenP, Width, 1, f[1]) != 1 ||
  232.         fread(BlueP, Width, 1, f[2]) != 1)
  233.         GIF_EXIT("Input file(s) terminated prematurly.");
  234.         RedP += Width;
  235.         GreenP += Width;
  236.         BlueP += Width;
  237.     }
  238.  
  239.     fclose(f[0]);
  240.     fclose(f[1]);
  241.     fclose(f[2]);
  242.     }
  243. }
  244.  
  245. /******************************************************************************
  246. * Save the GIF resulting image.                              *
  247. ******************************************************************************/
  248. static void SaveGif(GifByteType *OutputBuffer,
  249.             GifColorType *OutputColorMap,
  250.             int ExpColorMapSize, int Width, int Height)
  251. {
  252.     int i;
  253.     GifFileType *GifFile;
  254.     GifByteType *Ptr = OutputBuffer;
  255.  
  256.     /* Open stdout for the output file: */
  257.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  258.     QuitGifError(GifFile);
  259.  
  260.     if (EGifPutScreenDesc(GifFile,
  261.               Width, Height, ExpColorMapSize, 0, ExpColorMapSize,
  262.               OutputColorMap) == GIF_ERROR ||
  263.     EGifPutImageDesc(GifFile,
  264.              0, 0, Width, Height, FALSE, ExpColorMapSize, NULL) ==
  265.                                                                  GIF_ERROR)
  266.     QuitGifError(GifFile);
  267.  
  268.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  269.            PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  270.            GifFile -> IWidth, GifFile -> IHeight);
  271.  
  272.     for (i = 0; i < Height; i++) {
  273.     if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR)
  274.         QuitGifError(GifFile);
  275.     GifQprintf("\b\b\b\b%-4d", Height - i - 1);
  276.  
  277.     Ptr += Width;
  278.     }
  279.  
  280.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  281.     QuitGifError(GifFile);
  282. }
  283.  
  284. /******************************************************************************
  285. * Close output file (if open), and exit.                      *
  286. ******************************************************************************/
  287. static void QuitGifError(GifFileType *GifFile)
  288. {
  289.     PrintGifError();
  290.     if (GifFile != NULL) EGifCloseFile(GifFile);
  291.     exit(1);
  292. }
  293.